package vip.xumy.admin.um.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import vip.xumy.admin.um.mapper.IRolePermissionMapper;
import vip.xumy.admin.um.pojo.Role;
import vip.xumy.admin.um.service.IRolePermissionService;
import vip.xumy.core.pojo.com.Entry;

/**	Ownership belongs to the company
 *	author:mengyxu
 *	date:2019年12月3日
 */

@Service
public class RolePermissionService implements IRolePermissionService {
	@Autowired
	private IRolePermissionMapper rolePermissionMapper;

	@Override
	public List<Role> queryAll() {
		return rolePermissionMapper.queryAll();
	}
	
	@Override
	public List<Role> queryList() {
		return rolePermissionMapper.queryList();
	}

	@Override
	@Transactional
	public void save(Role role) {
		role.setStatus("E");
		rolePermissionMapper.insertOne(role);
		if(role.getPermissions() != null && !role.getPermissions().isEmpty()) {
			rolePermissionMapper.insertRolePer(role);
		}
	}
	
	@Override
	@Transactional
	public void delete(String roleCode) {
		rolePermissionMapper.delete(roleCode);
		rolePermissionMapper.deleteRolePer(roleCode);
	}

	@Override
	@Transactional
	public String update(Role role) {
		if(role.isFlag()) {
			rolePermissionMapper.deleteRolePer(role.getCode());
			if(role.getPermissions() != null && !role.getPermissions().isEmpty()) {
				rolePermissionMapper.insertRolePer(role);
			}
			return "角色权限分配成功";
		}else {
			rolePermissionMapper.update(role);
			return "角色修改成功";
		}
	}

	@Override
	public List<Entry<String, String>> queryRoleMaps() {
		return rolePermissionMapper.queryRoleMaps();
	}

}
